package com.jsfcompref.solutech.backing;


import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputSecret;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import com.eCommerce.corecontract.dto.AdminDTO;
import com.eCommerce.corecontract.dto.ClientDTO;
import com.eCommerce.corecontract.dto.UserDTO;
import com.eCommerce.server.corebackend.util.BackEndConstants;
import com.eCommerce.server.corebackend.util.UtilDate;
import com.eCommercecorecontract.svco.IUserSVCO;
import com.eCommercecorecontract.svco.SVCOFinder;
import com.ecommerceWeb.common.MessageFactory;
import com.ecommerceWeb.common.ServiceFinder;


/**
 * Backing bean for register.jsp
 */



public class Register 
{
  
  private HtmlInputText firstName;
  private HtmlInputText lastName;
  private HtmlInputText email;
  private Date dob;
  private HtmlInputText address;
  private HtmlInputText phoneNum;
  private HtmlInputText userid;
  private HtmlInputSecret password;
  private HtmlInputSecret passwordCheck;
 
  

  public Register()
  {
  }

  public void validateEmail(FacesContext context, UIComponent toValidate,
                            Object value) throws ValidatorException {
        boolean isValid = false;
        String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$"; 
        Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher((String) value);
        if(matcher.matches()){  
        	isValid=true;
        }else{
        	isValid=false;
        	FacesMessage message = new FacesMessage("Invalid email address!");
            throw new ValidatorException(message);
        }
  }
  public void validatePassword(FacesContext context, UIComponent toValidate,
                            Object value) throws ValidatorException {
    
    String password1 = (String)this.getPassword().getValue();
    String password2 = (String) value;
        
    if(!password1.equals(password2)) {
      FacesMessage message = new FacesMessage("Passwords do not match!");
      throw new ValidatorException(message);
    }
  }
  
  public void validatePhoneNumber(FacesContext context, UIComponent toValidate,
          Object value) throws ValidatorException {
	  boolean isValid = false;
	  String expression = "^[0-9]*$"; 
	  Pattern pattern = Pattern.compile(expression);
	  Matcher matcher = pattern.matcher((String) value);
	  if(matcher.matches()){
		  isValid = true;  
	  }else{
		  isValid = false; 
		  FacesMessage message = new FacesMessage("Invalid phone number!");
          throw new ValidatorException(message);
	  }
  }

  
 public String registerUser()
 {
	 	String status="failure" ;
		try {
			UserDTO clientDTO = new ClientDTO();
			clientDTO.setPrenom(firstName.getValue().toString());
			clientDTO.setNom(lastName.getValue().toString());
			clientDTO.setDateNaissance(UtilDate.ConvertDateToLong((Date) dob
					));
			clientDTO.setEmail(email.getValue().toString());
			clientDTO.setAdresse(address.getValue().toString());
			clientDTO.setTelephone(phoneNum.getValue().toString());
			clientDTO.setLogin(userid.getValue().toString());
			clientDTO.setPassword(password.getValue().toString());
			clientDTO.setPassword(passwordCheck.getValue().toString());
			SVCOFinder svcoFinder = (SVCOFinder) ServiceFinder
					.findBean("SVCOFinder");
			IUserSVCO clientSVCO = (IUserSVCO) svcoFinder
					.get(BackEndConstants.SVCO_USER);
			Long clientCreated = clientSVCO.creerUser(clientDTO);
			status=MessageFactory.status_confirm;

		} catch (Exception e) {
			// TODO Auto-generated catch block
	
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Erreur survenue lors de la creation du compte", null));
			status=MessageFactory.status_client_failure;
			e.printStackTrace();
			
		}
		return status;
  
  }

public Date getDob() {
	return dob;
}

public void setDob(Date dob) {
	this.dob = dob;
}

public HtmlInputText getAddress() {
	return address;
}

public void setAddress(HtmlInputText address) {
	this.address = address;
}

public HtmlInputText getPhoneNum() {
	return phoneNum;
}

public void setPhoneNum(HtmlInputText phoneNum) {
	this.phoneNum = phoneNum;
}

  public void setFirstName(HtmlInputText firstName) {
    this.firstName = firstName;
  }

  public HtmlInputText getFirstName() {
    return firstName;
  }

  public void setLastName(HtmlInputText lastName) {
    this.lastName = lastName;
  }

  public HtmlInputText getLastName() {
    return lastName;
  }



  public void setEmail(HtmlInputText email) {
    this.email = email;
  }

  public HtmlInputText getEmail() {
    return email;
  }


  public void setUserid(HtmlInputText userid) {
    this.userid = userid;
  }

  public HtmlInputText getUserid() {
    return userid;
  }

  public void setPassword(HtmlInputSecret password) {
    this.password = password;
  }

  public HtmlInputSecret getPassword() {
    return password;
  }



  public void setPasswordCheck(HtmlInputSecret passwordChk) {
    this.passwordCheck = passwordChk;
  }

  public HtmlInputSecret getPasswordCheck() {
    return passwordCheck;
  }





  

}
